import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_day_stat.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/utils/duration_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 4/5/21.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 机考记录
class MyPcExamRecoderPage extends StatefulWidget {
  @override
  _MyPcExamRecoderPageState createState() => _MyPcExamRecoderPageState();
}

class _MyPcExamRecoderPageState extends State<MyPcExamRecoderPage> {
  @override
  void initState() {
    super.initState();
    _getTopicHistoryist();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("机考记录"),
      ),
      backgroundColor: Color(0xfff2f2f2),

      ///填充布局
      body: Container(padding: EdgeInsets.all(12), child: buildListView()),
    );
  }

  bool _isLoading = true;

  Widget buildListView() {
    if (_isLoading) {
      return LoadingWidget();
    }
    if (_beanList.length == 0) {
      setState(() {
        _isLoading = true;
      });
      return Center(
        child: NoDataWidget(
          clickCallBack: () {
            _getTopicHistoryist();
          },
        ),
      );
    }
    return RefreshIndicator(
      onRefresh: () async {
        await _getTopicHistoryist();
        return true;
      },
      child: ListView.builder(
          itemCount: _beanList.length,
          itemBuilder: (BuildContext context, int index) {
            BeanDayStat beanDayStat = _beanList[index];
            double obtainScore = beanDayStat.obtainScore / 10;
            Color textColor = Colors.red;
            String time = beanDayStat.createTime;
            double top = 6.0;
            if (index > 0) {
              BeanDayStat pre = _beanList[index - 1];
              String preTime = pre.createTime;
              if (time.substring(0, 10) == preTime.substring(0, 10)) {
                top = 0.0;
              }
            }
            return Container(
              padding: EdgeInsets.all(10),
              margin: EdgeInsets.only(top: top),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(4)),
                  color: Colors.white),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  top != 0
                      ? Container(
                          margin: EdgeInsets.only(bottom: 10),
                          child: Row(
                            children: [
                              Container(
                                height: 16,
                                width: 3,
                                decoration: BoxDecoration(
                                    color: Colors.green,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(4))),
                                margin: EdgeInsets.only(right: 10),
                              ),
                              Text(
                                "${time.substring(0, 10)}",
                                style: TextStyle(
                                    fontWeight: FontWeight.w500,
                                    fontSize: 16,
                                    color: Color(0xff444444)),
                              )
                            ],
                          ),
                        )
                      : Container(),
                  Text("考试时间：$time"),
                  SizedBox(
                    height: 6,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text("最终得分：${obtainScore} 分"),
                      SizedBox(
                        width: 20,
                      ),
                      Text(
                          "用时：${DurationUtils.fromatMil(beanDayStat.answerTime)}"),
                    ],
                  )
                ],
              ),
            );
          }),
    );
  }

  List<BeanDayStat> _beanList = [];

  Future<List<BeanDayStat>> _getTopicHistoryist() async {
    Map<String, dynamic> monthMap = {
      "categoryOne": "COMPUTER_TEST",
      "sort": "TIMES",
      "pageName": "MyPcExamRecoderPage",
    };

    ResponseInfo statisRresponseInfo = await DioUtils.instance.postRequest(
        url: HttpHelper.myMyScoreQuestionRecoderUrl, jsonMap: monthMap);
    _beanList = [];
    if (statisRresponseInfo.success) {
      List list = statisRresponseInfo.data['list'];
      list.forEach((element) {
        _beanList.add(BeanDayStat.fromMap(element));
      });
      _isLoading = false;
      if (mounted) {
        setState(() {});
      }
      return _beanList;
    } else {
      ToastUtils.showToast(statisRresponseInfo.message);
      _isLoading = false;
      if (mounted) {
        setState(() {});
      }

      return _beanList;
    }
  }
}
